package pub.guoxin.oops.manager.service.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import pub.guoxin.oops.manager.service.CommunicationService;
import pub.guoxin.oops.manager.vo.RealTimeCommunicationVo;
import pub.guoxin.oops.model.QRealTimeCommunication;
import pub.guoxin.oops.model.RealTimeCommunication;
import pub.guoxin.oops.model.RealTimeDeviceDataInfo;
import pub.guoxin.oops.model.User;
import pub.guoxin.oops.repository.RealTimeCommunicationRepository;
import pub.guoxin.oops.repository.RealTimeDeviceDataInfoRepository;
import pub.guoxin.oops.vo.ResultVo;
import pub.guoxin.oops.vo.ResultVoBuilder;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * Created by guoxin on 18-1-25.
 */
@Service
public class CommunicationServiceImpl implements CommunicationService {

    @Autowired
    private RealTimeCommunicationRepository realtimeCommunicationRepository;
    @Autowired
    private RealTimeDeviceDataInfoRepository realTimeDeviceDataInfoRepository;
    @Autowired
    private ResultVoBuilder builder;

    /**
     * TODO
     *
     * @param user
     * @param sn
     * @param groupId
     * @return
     */
    @Override
    public ResultVo register(User user, String sn, String groupId) {
        RealTimeCommunication realtimeCommunication = realtimeCommunicationRepository.findOne(sn);
        HashMap<String, Boolean> map = Maps.newHashMap();
        if (Objects.isNull(realtimeCommunication)) {
            map.put("success", false);
            return builder.buildInfo(200, "未找到数据", map);
        }
        if (!Strings.isNullOrEmpty(realtimeCommunication.getGroupId())) {
            return builder.buildBadRequestResultInfo("注册失败，该通讯模块已经被注册");
        }
        realtimeCommunication.setCompanyId(user.getCompanyId());
        realtimeCommunication.setGroupId(groupId);
        realtimeCommunicationRepository.save(realtimeCommunication);
        map.put("success", true);
        return builder.buildInfo(200, "注册成功", map);
    }

    @Override
    public Page<RealTimeCommunicationVo> page(User user, String groupId, Integer pageNumber, Integer pageSize) {
        QRealTimeCommunication q = QRealTimeCommunication.realTimeCommunication;
        BooleanExpression expression = q.groupId.eq(groupId);

        Pageable request = new PageRequest(pageNumber - 1, pageSize, Sort.Direction.DESC, "creation");
        Page<RealTimeCommunication> all = realtimeCommunicationRepository.findAll(expression, request);

        return all.map(realTimeCommunication -> {
            List<RealTimeDeviceDataInfo> devices = realTimeDeviceDataInfoRepository.
                    findByRealTimeCommunicationId(realTimeCommunication.getId());
            return new RealTimeCommunicationVo(realTimeCommunication, devices);
        });
    }

    @Override
    public boolean modifyRemark(String sn, String remark) {
        RealTimeCommunication one = realtimeCommunicationRepository.findOne(sn);
        if (Objects.isNull(one)) {
            return false;
        }
        one.setRemark(remark);
        realtimeCommunicationRepository.save(one);
        return true;
    }

}
